package com.winbomb.kingcard.base.anim.impl;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.base.AnimManager;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.base.GameState;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.base.anim.TransAnimation;
import com.winbomb.kingcard.framework.Graphics;
import com.winbomb.kingcard.screens.util.Render;
import com.winbomb.kingcard.utils.GameUtils;

public class MarchAnim implements IPlayable {

	public static final int FACE_NORTH = 0;
	public static final int FACE_SOUTH = 1;
	public static final int FACE_WEST = 3;
	public static final int FACE_EAST = 4;

	private static final int FRAME_W = 24;
	private static final int FRAME_H = 24;

	private TransAnimation transAnim;
	private IGameLogic game;
	private Render render;
	private Graphics g;
	private int direction;

	public MarchAnim(IGameLogic game) {
		this.game = game;
		this.render = ((GameLogic) game).getRender();
		this.g = render.getGraphics();
	}

	@Override
	public boolean play(float dt) {
		if (!transAnim.hasFinished()) {
			int frameNum = transAnim.getCurrFrame();

			int y = 0;
			switch (direction) {
			case FACE_SOUTH:
				y = 0;
				break;
			case FACE_EAST:
				y = FRAME_H;
				break;
			case FACE_NORTH:
				y = FRAME_H * 2;
				break;
			case FACE_WEST:
				y = FRAME_H * 3;
				break;
			}
			g.drawPixmap(Assets.warrior, (int) transAnim.getCurrX(), (int) transAnim.getCurrY(), frameNum * FRAME_W, y,
					FRAME_W, FRAME_H);
			transAnim.step(dt);
			return false;
		}

		IPlayer currTurn = game.getCurrTurn();
		IPlayer currTarget = currTurn.getAttackRoute().getCurrTarget();
		if (currTarget == currTurn.getAttackTarget()) {
			// 轮到进攻的目标选择出击的牌了
			IPlayer target = game.getCurrTurn().getAttackTarget();
			game.setCurrPlayer(target);
			game.setState(GameState.SELECT_DEF_CARDS);
		} else if (currTarget.isAlive() && !GameUtils.isSameForce(currTurn, currTarget)) {
			game.setCurrPlayer(currTarget);
			game.setState(GameState.BEGIN_NEGOTIATE);
		} else {
			currTurn.getAttackRoute().move();
			((GameLogic) game).getAnimManager().addAnimation(AnimManager.ANIM_MARCH);
		}

		return true;
	}

	public void reset() {

	}

	public void newMarch(IPlayer currLoc, IPlayer currTarget) {

		float x0 = currLoc.getPlayerInfo().getCampPos().x;
		float y0 = currLoc.getPlayerInfo().getCampPos().y;
		float x1 = currTarget.getPlayerInfo().getCampPos().x;
		float y1 = currTarget.getPlayerInfo().getCampPos().y;

		transAnim = new TransAnimation(x0, y0, x1, y1);
		transAnim.setFrameCount(5);
		transAnim.setDuration(1000);

		if (x1 > x0 && (x1 - x0 > y1 - y0 && x1 - x0 > y0 - y1)) {
			direction = FACE_EAST;
		} else if (x0 > x1 && (x0 - x1 > y1 - y0 && x0 - x1 > y0 - y1)) {
			direction = FACE_WEST;
		} else if (y1 > y0 && (y1 - y0 > x1 - x0 && y1 - y0 > x0 - x1)) {
			direction = FACE_SOUTH;
		} else if (y0 > y1 && (y0 - y1 > x1 - x0 && y0 - y1 > x0 - x1)) {
			direction = FACE_NORTH;
		}

	}
}
